<template>
  <el-form label-width="108px">
    <el-form-item label="选择任务" prop="taskId">
      <el-select
        v-model="formData.taskId"
        clearable
        placeholder="请选择"
        @change="(val) => taskChange(val, 'taskId')"
      >
        <el-option
          v-for="taskItem in taskList"
          :key="taskItem.id"
          :label="taskItem.name"
          :value="taskItem.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="选择测点" prop="measureId">
      <el-select
        v-model="formData.measureId"
        clearable
        placeholder="请选择"
        @change="(val) => measureChanged(val, 'measureId')"
      >
        <el-option
          v-for="measureItem in measureOptions"
          :key="measureItem.id"
          :label="measureItem.name"
          :value="measureItem.id"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="逻辑传感器" prop="signalId">
      <el-select
        v-model="formData.signalId"
        clearable
        placeholder="请选择"
        @change="(val) => logicSensorChanged(val, 'signalId')"
      >
        <el-option
          v-for="item in logicSensorsOptions"
          :key="item.signalId"
          :label="item.signalName + '@' + measureName"
          :value="item.signalId"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="刷新间隔(毫秒)" prop="refreshTime">
      <el-input-number
        v-model="formData.refreshTime"
        size="default"
        style="width: 100%"
        controls-position="right"
        @change="changed($event, 'refreshTime')"
      />
    </el-form-item>
  </el-form>
</template>
<script lang="ts" setup>
import { reactive, watch } from 'vue'
import { TaskApi } from '@/api/vmiot/task'

// 接收父组件传入的值
const props = defineProps<{
  modelValue: any
}>()

const emit = defineEmits(['update:modelValue', 'onChanged'])

const taskList = ref<any[]>([])

const formData = reactive<any>({})

const measureOptions = ref<any[]>([])

const logicSensorsOptions = ref<any[]>([])

const taskChange = (val: any, key: string) => {
  changed(val, key)
  getMeasureOptions()
}

const measureChanged = (val: any, key: string) => {
  changed(val, key)
  getLogicSensorOptions()
}

const logicSensorChanged = (val: any, key: string) => {
  const signal = logicSensorsOptions.value.find((item) => item.signalId === val)
  Object.assign(formData, {
    signalId: val,
    signal: {
      logicSensorName: measureName.value,
      ...signal
    }
  })
  changed(val, key)
}

// 计算当前任务的测点名称
const measureName = computed(() => {
  return measureOptions.value.find((item) => item.id === formData.measureId)?.name
})

const getTaskList = async () => {
  const res = await TaskApi.getTaskPage({
    pageNo: 1,
    pageSize: 100,
    signalType: props.modelValue.signalType
  })
  taskList.value = res.list
  if (formData.taskId) {
    getMeasureOptions()
  }
}

// 根据任务id获取测点列表
const getMeasureOptions = () => {
  const task = taskList.value.find((item) => item.id === formData.taskId)
  const { nodes } = JSON.parse(task.content)

  const measureNodes = nodes
    .filter((item) => item.type === 'output')
    .map((item) => {
      return {
        id: item.id,
        ...item.data.measureData
      }
    })

  measureOptions.value = measureNodes
  if (formData.measureId) {
    getLogicSensorOptions()
  }
}

// 根据测点id获取逻辑传感器列表
const getLogicSensorOptions = () => {
  const measure = measureOptions.value.find((item) => item.id === formData.measureId)
  // 过滤出当前信号类型下的逻辑传感器
  logicSensorsOptions.value =
    measure?.signalList.filter((item) => item.signalType === props.modelValue.signalType) || []
}

watch(
  () => props.modelValue,
  (newVal) => {
    Object.assign(formData, newVal)
  },
  { immediate: true, deep: true }
)

onMounted(() => {
  getTaskList()
})

const changed = (val: any, key: string) => {
  emit('update:modelValue', { ...formData })
  emit('onChanged', { key, val })
}
</script>
<style lang="scss" scoped></style>
